<template>
	<view>
					<view class="pay-container1">
									<view class="pay">
													<view class="radio">
																		<radio  :checked="allChecked" @click="allCheckedHandler" style="margin-right: 4rpx;" color="#C00000"></radio><text>全选</text>
													</view>
													<view  class="total">
																		合计:<text>¥{{totalMoney.toFixed(2)}}</text>
													</view>
													<view class="">
																		<view class="goPay" @click="goPay">
																							结算({{payCount}}) 
																		</view>
													</view>
									</view>
					</view>
					
					<view>
								<!-- 提示窗示例 -->
								<uni-popup ref="alertDialog" type="dialog">
									<uni-popup-dialog type="info" cancelText="取消" confirmText="登录" title="温馨提示" content="您还未登录,请点击登录" @confirm="dialogConfirm"
										></uni-popup-dialog>
								</uni-popup>
					</view>
					
	</view>
</template>

<script>
			import { mapGetters ,mapMutations ,mapState } from 'vuex'
	export default {
		name:'settle',
		data() {
			return {
						// allChecked:false,
						payCount:0,
						flag:false
			}
		},
		props:{
				cartList:{
						type:Array,
						default:[]
				}
		},
		computed:{ 
				...mapGetters('cart',['toPay','totalMoney']) ,
				...mapState('cart',['allChecked','cart']),
				...mapGetters('user',['addStr']) ,
				...mapState('user',['address','token'])
		},
		watch:{
					toPay:{
								handler(newVal){
											this.payCount = newVal
								},
								immediate:true
					},
		},
		methods:{
						
						...mapMutations('cart',['updateCartLsit','changeAllChecked']),
						...mapMutations('user',['setRedirectInfo']),
						/**
						 * 跳转登陆页
						 */
						dialogConfirm() {
									this.setRedirectInfo('/pages/cart/cart')
									uni.switchTab({ url:'/pages/my/my' })
						},
						/**
						 * 	去结算
						 */
						goPay(){
									if(this.toPay <=0 ){
												uni.$showMsg('请勾选结算商品')
									}else if(JSON.stringify(this.address) === '{}'){
												uni.$showMsg('请选择收货地址')
									}else if(!this.token){
												this.$refs.alertDialog.open()
									}else{
												
												this.payOrder()
												
									}
						},
						/**
						 * 创建订单去支付
						 */
						async payOrder(){
									if(this.flag) return 
									this.flag = true
									const timeId = setTimeout(async()=>{
															//1.创建订单
																const query = {
																		order_price:this.totalMoney,
																		consignee_addr:this.addStr,
																		goods:this.cart.filter(item=>item.goods_state===true).map(item=>{
																					return {
																								goods_id:item.goods_id,
																								goods_number:item.goods_count,
																								goods_price:0.01
																					}
																		})
																}
																console.log('query',query)
																const { data: createOrderRes } = await uni.$http.post('/api/public/v1/my/orders/create',query)
																if(createOrderRes.meta.status !== 200) return uni.$showMsg('创建订单失败') 
																
																console.log('createOrderRes',createOrderRes)
																//2.订单预支付
																  //2.1构建预支付参数
																	const { data:prePayRes } = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder',{ order_number:createOrderRes.message.order_number })
																	console.log('prePayRes',prePayRes)
																	const payInfo = {
																						'timeStamp': '1564730510',
																						'nonceStr': 'SReWbt3nEmpJo3tr',
																						'package': 'prepay_id=wx02152148991420a3b39a90811023326800',
																						'signType': 'MD5',
																						'paySign': '3A6943C3B865FA2B2C825CDCB33C5304'
																	}
																//3.发起微信支付
															uni.requestPayment({
																	payInfo,
																	success(res) {console.log(res)},
																	fail(e) {console.log(e)}
															})
															this.flag = false
									},1000)
								
								
								
						},
					/**
					 * 全选
					 */
					allCheckedHandler(){
								this.changeAllChecked(!this.allChecked)
								this.cartList.forEach(item=>{
											item.goods_state = this.allChecked
								})
								this.updateCartLsit(this.cartList)
					},
		}
	}
</script>

<style lang="scss">
			.pay-container1{
							
							.pay{
								font-size: 28rpx;
									width: 100%;
									height: 110rpx;
									display: flex;
									align-items: center;
									justify-content: space-between;
									bottom: 0;
									position: fixed;
									background-color: white;
									border-top: 1px solid #e0e7ed;
											.radio{
														margin-left: 20rpx;
											}
											.total{
												text{
														color: #b02418;
												}
											}
											.goPay{
														color: white;
														background-color: #c70000;
														width: 240rpx;
														height: 110rpx;
														line-height: 110rpx;
														text-align: center;
														font-weight: 200;
											}
							}
			}
</style>